﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using WorkTimePrj.Models;
using WsValid;
using System.Web.BizCode;

namespace WorkTimePrj.Controllers
{
    public class PMController : Controller
    {
        private WorkTimePrjEntities3 db = new WorkTimePrjEntities3();

        //
        // GET: /PM/
        [WSAuth(Roles = "2c05d336-4e01-420d-9081-14386430779b")]
        public ViewResult Index(DateTime? startTime = null, DateTime? endTime = null)
        {
            if (startTime == null || endTime == null || (endTime.Value - startTime.Value).Days >= 7)
            {
                int dayOfWeek = (int)DateTime.Today.DayOfWeek;
                if (dayOfWeek == 0)
                {
                    startTime = DateTime.Today.AddDays(-6);
                }
                else
                {
                    startTime = DateTime.Today.AddDays(-1 * dayOfWeek + 1);
                }
                endTime = startTime.Value.AddDays(+6);
            }
            ViewBag.startTime = startTime;
            ViewBag.endTime = endTime;
            //WorkTimePrj.Models.User user = db.User.Single(i => i.Name == Consts.CurrentUser.Name);
            //string curuser_displayname = user.DisplayName;
            var item = db.Item.Include("Project").Where(i => i.Project.PM == Consts.CurrentUser.DisplayName && i.starttime == startTime && i.ack == null);
            //var item = db.Item.Include("Project").Where(i => i.PM_Name == Consts.CurrentUser.Name);
            List<Item> list = item.ToList();
            ViewBag.cnt = list.Count();
            return View(list);
        }

        //
        // GET: /PM/Details/5

        public ViewResult Details(Guid id)
        {
            Item item = db.Item.Single(i => i.uid == id);
            return View(item);
        }

        //
        // GET: /PM/Create

        public ActionResult Create()
        {
            ViewBag.pid = new SelectList(db.Project, "pid", "code");
            return View();
        }

        //
        // POST: /PM/Create

        [HttpPost]
        public ActionResult Create(Item item)
        {
            if (ModelState.IsValid)
            {
                item.uid = Guid.NewGuid();
                db.Item.AddObject(item);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.pid = new SelectList(db.Project, "pid", "code", item.pid);
            return View(item);
        }

        //
        // GET: /PM/Edit/5

        public ActionResult Edit(Guid id)
        {
            Item item = db.Item.Single(i => i.uid == id);
            ViewBag.pid = new SelectList(db.Project, "pid", "code", item.pid);
            return View(item);
        }

        //
        // POST: /PM/Edit/5
        [HttpPost]
        [WSAuth(Roles = "2c05d336-4e01-420d-9081-14386430779b")]
        public ActionResult Edit(Item item, DateTime startTime, DateTime endTime)
        {
            //if (ModelState.IsValid)
            //{
            //Item item1 = db.Item.Single(i => i.uid == item.uid);
            item.ack_user = Consts.CurrentUser.Name;
            item.ack = 1;
            item.ack_time = DateTime.Now;
            item.ack_type = "PM确认";
            db.Item.Attach(item);

            db.ObjectStateManager.ChangeObjectState(item, EntityState.Modified);
            db.SaveChanges();
            this.ShowMessage("保存成功");
            return RedirectToAction("Index", new { startTime = startTime, endTime = endTime });
            //}
            //ViewBag.pid = new SelectList(db.Project, "pid", "code", item.pid);
            //return View(item);
        }

        //
        // GET: /PM/Delete/5

        public ActionResult Delete(Guid id)
        {
            Item item = db.Item.Single(i => i.uid == id);
            return View(item);
        }

        //
        // POST: /PM/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {
            Item item = db.Item.Single(i => i.uid == id);
            db.Item.DeleteObject(item);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        [WSAuth(Roles = "ba144991-0307-4a52-b6e8-ca7b8fc3b5b9")]
        public ViewResult List(DateTime? startTime = null, DateTime? endTime = null)
        {
            if (startTime == null || endTime == null || (endTime.Value - startTime.Value).Days >= 7)
            {
                int dayOfWeek = (int)DateTime.Today.DayOfWeek;
                if (dayOfWeek == 0)
                {
                    startTime = DateTime.Today.AddDays(-6);
                }
                else
                {
                    startTime = DateTime.Today.AddDays(-1 * dayOfWeek + 1);
                }
                endTime = startTime.Value.AddDays(+6);
            }
            ViewBag.startTime = startTime;
            ViewBag.endTime = endTime;
           
            List<Item> item = db.Item.Include("Project").Where(i => i.Project.PM == Consts.CurrentUser.DisplayName && i.starttime < startTime && i.ack == null).ToList();
           
            ViewBag.cnt = item.Count();
            List<Item> list = item.ToList();
            int r = 0;
            foreach (Item it in list)
            {
                r++;
                it.rowNum = r;
            }
            return View(item.ToList());
        }

        [HttpPost]
        [WSAuth(Roles = "ba144991-0307-4a52-b6e8-ca7b8fc3b5b9")]
        public ActionResult List(IEnumerable<Item> items)
        {
            //if (ModelState.IsValid)
            //{
            //Item item1 = db.Item.Single(i => i.uid == item.uid);
            string userName = Consts.CurrentUser.Name;
            foreach (Item item in items)
            {
                item.ack_user = userName;
                item.ack = 1;
                item.ack_time = DateTime.Now;
                item.ack_type = "系统确认";
                db.Item.Attach(item);

                db.ObjectStateManager.ChangeObjectState(item, EntityState.Modified);

            }
            db.SaveChanges();
            this.ShowMessage("确认成功");
            return RedirectToAction("List");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}